package com.iretailer.report.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iretailer.report.dto.PageResult;
import com.iretailer.report.dto.QueryOption;
import com.iretailer.report.mapper.CameraZoneMapper;
import com.iretailer.report.model.CameraZone;
import com.iretailer.report.model.CameraZoneCamera;
import com.iretailer.report.model.CameraZoneMd;
import com.iretailer.report.model.PhysicalZoneMall;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: xjw
 * @Date: 2019/1/23 16:30
 * @Description:
 */
@Service
public class CameraZoneService extends ServiceImpl<CameraZoneMapper, CameraZone> {
    @Autowired
    private CameraZoneMapper cameraZoneMapper;
    @Autowired
    private PhysicalzonesService physicalzonesService;


    /**
     * 根据 MallId  enabled
     */
    public List<CameraZoneCamera> selectCameraZoneByEnabled_MallId(Integer enabled, String mallId) {
        return cameraZoneMapper.selectCameraZoneByEnabled_MallId(enabled, mallId);
    }

    public List<CameraZoneCamera> selectCameraZoneByMallId_ZoneClass_Enable(String mallId, String zoneClass, Integer enabled) {
        return cameraZoneMapper.selectCameraZoneByMallId_ZoneClass_Enable(mallId, zoneClass, enabled);
    }


    public List<CameraZone> selectCameraZoneByEnabled(Integer enabled) {
        return cameraZoneMapper.selectCameraZoneByEnabled(enabled);
    }


    public List<CameraZoneCamera> selectCameraZoneByEnabled_PhysicalZoneId(Integer enabled, Long physicalZoneId) {
        return cameraZoneMapper.selectCameraZoneByEnabled_PhysicalZoneId(enabled, physicalZoneId);
    }

    public List<CameraZoneCamera> selectByEnabled_PhysicalZoneIds(Integer enabled, List<Long> physicalZoneIds) {
        return cameraZoneMapper.selectByEnabled_PhysicalZoneIds(enabled, physicalZoneIds);
    }

    /**
     * 根据 deviceId 查询
     */
    public List<CameraZoneCamera> selectCameraByDeviceId(String deviceId) {
        return cameraZoneMapper.selectCameraByDeviceId(deviceId);
    }

    /**
     * 主要用于解析入库，
     * 返回 zoneId 和 CameraZone 的 Map
     */
    @Cacheable(value = "cameraZonesMap", key = "#deviceId")
    public Map<String, CameraZoneCamera> cameraZonesMap(String deviceId) {
        List<CameraZoneCamera> cameraZones = selectCameraByDeviceId(deviceId);
        if (cameraZones != null) {
            Map<String, CameraZoneCamera> result = new HashMap<>();
            cameraZones.forEach(it -> {
                result.put(it.getZone(), it);
            });
            return result;
        } else {
            return null;
        }
    }

    @CacheEvict(value = {"cameraZonesMap"}, allEntries = true)
    public boolean saveOrUpdate(CameraZone cameraZone, String userName) {

        Long id = cameraZone.getId();
        cameraZone.setUpdateTime(LocalDateTime.now());
        cameraZone.setUpdateUser(userName);
        if (id != null) {
            cameraZoneMapper.updateById(cameraZone);
            return true;
        } else {
            cameraZoneMapper.insert(cameraZone);
            return true;
        }
    }

    @CacheEvict(value = {"cameraZonesMap"}, allEntries = true)
    public boolean deleteById(Long id) {
        List<PhysicalZoneMall> physicalZones = physicalzonesService.selectPhysicalZonesByEnabled_CameraZoneId(null, id);
        if (physicalZones != null && physicalZones.size() > 0) {
//            return "已关联区域，不能删除"
            return false;
        } else {
            cameraZoneMapper.deleteById(id);
            return true;
        }
    }

    public PageResult<CameraZoneMd> grid(String userId, Integer enable, String mallName, String zoneClass, QueryOption queryOption) {
        Page page = new Page<CameraZoneMd>(1, Long.MAX_VALUE);
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("1", 1);
        if (enable != null) {
            queryWrapper.eq("enabled", enable);
        }
        if (mallName != null) {
            queryWrapper.like("mallName", mallName);
        }
        if (zoneClass != null) {
            queryWrapper.like("ZoneClass", zoneClass);
        }
        queryOption.judge(page, queryWrapper);
        if (userId == null) {
            cameraZoneMapper.selectPageCameraZone(page, queryWrapper);
        } else {
            queryWrapper.eq("userId", userId);
            cameraZoneMapper.selectPageUserCameraZone(page, queryWrapper);
        }
        return new PageResult<>(page.getTotal(), page.getRecords());
    }


    public List<CameraZoneMd> listCameraZone(String userId, Integer enable, String mallName, String zoneClass, QueryOption queryOption) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("1", 1);
        if (enable != null) {
            queryWrapper.eq("enabled", enable);
        }
        if (mallName != null) {
            queryWrapper.like("mallName", mallName);
        }
        if (zoneClass != null) {
            queryWrapper.like("ZoneClass", zoneClass);
        }
        queryOption.judge(queryWrapper);
        if (userId == null) {
            return cameraZoneMapper.selectCameraZone(queryWrapper);
        } else {
            queryWrapper.eq("userId", userId);
            return cameraZoneMapper.selectUserCameraZone(queryWrapper);
        }
    }
}
